package gemini.castor.ui.client.page.content.account.volumepoint.form;

import gemini.basic.model.MonthlyPoint;
import gemini.castor.ui.client.ServiceCatalog;
import gemini.castor.ui.client.mvp.callback.AbstractAsyncCallback;
import gemini.castor.ui.client.mvp.single.CastorWidgetDisplay;
import gemini.castor.ui.client.mvp.single.CastorWidgetPresenter;
import gemini.castor.ui.client.page.RootEvent;
import gemini.castor.ui.client.utils.PlaceConstants;

import java.util.ArrayList;
import java.util.List;

import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.inject.Inject;

public class VolumePointFormPresenter extends
		CastorWidgetPresenter<VolumePointFormPresenter.Display> {

	@Inject
	public VolumePointFormPresenter(Display display, EventBus globalEventBus) {
		super(display, globalEventBus);
	}

	public interface Display extends CastorWidgetDisplay {

		int getYear();

		void loadData(List<VolumePointFormModel> models);

		void initData(List<Integer> years);

		HasChangeHandlers getListBoxChangeHandler();

		void showCurrentPoint(List<VolumePointFormModel> models);
	}

	private String distributorCode;
	
	@Override
	public void setInputObject(Object ob) {
		distributorCode = (String) ob;
	}

	public static final Place PLACE = new Place(
			PlaceConstants.PLACE_VOLUME_POINT);

	@Override
	protected void onBind() {
		getAllYears();
		
		registerHandler(display.getListBoxChangeHandler().addChangeHandler(
                new ChangeHandler() {
                        
                        @Override
                        public void onChange(ChangeEvent arg0) {
                        	eventBus.fireEvent(new RootEvent(
									RootEvent.ForwardType.MASK));
                        	getVolumePoint(distributorCode, display.getYear());
                        }
                }));
	}

	private void getVolumePoint(String code, int year) {
		ServiceCatalog.getMonthlyPointService()
				.getMonthlyPointByDistributorCodeAndYear(code, year,
						new AbstractAsyncCallback<List<MonthlyPoint>>() {

							@Override
							public void onSuccess(List<MonthlyPoint> result) {
								eventBus.fireEvent(new RootEvent(
										RootEvent.ForwardType.UN_MASK));
								if (result != null && !result.isEmpty()) {
									display.loadData(createModelFromResult(result));
									if(firstLoad){
										display.showCurrentPoint(createModelFromResult(result));
										firstLoad = false;
									}
								}
							}
						});
	}
	
	private boolean firstLoad = false;

	private void getAllYears() {
		eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.MASK));
		
		ServiceCatalog.getMonthlyPointService().getAllYearByDistributorCode(
				distributorCode, new AbstractAsyncCallback<List<Integer>>() {

					@Override
					public void onSuccess(List<Integer> result) {
						if (result != null && !result.isEmpty()) {
							display.initData(result);
							firstLoad = true;
							getVolumePoint(distributorCode, display.getYear());
						}
					}
				});
	}

	@Override
	public Place getPlace() {
		return PLACE;
	}

	private List<VolumePointFormModel> createModelFromResult(
			List<MonthlyPoint> monthlyPoints) {
		List<VolumePointFormModel> models = new ArrayList<VolumePointFormModel>();
		for (MonthlyPoint monthlyPoint : monthlyPoints) {
			VolumePointFormModel model = new VolumePointFormModel();
			model.setMonth(monthlyPoint.getMonth());
			model.setGroupPoint(monthlyPoint.getGroupPoint());
			model.setPersonPoint(monthlyPoint.getPersonalPoint());
			models.add(model);
		}
		return models;
	}
}
